package it.goc.adunanza;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Timer;
import java.util.TimerTask;

import android.app.IntentService;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.*;
import android.widget.Toast;

public class service48GOC extends Service {
	
	
	public service48GOC() {
		// TODO Auto-generated constructor stub
		int Inizio  = 1000;
		int RipetiOgni  = 10000;		
		Timer T = new Timer();
		TimerTask Tt = new TimerTask() {			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Log.i("TAG DELSERVICE ","STO ANDANO EUREKA!!!!");
				URL url = null;
				try{
				url = new URL("http://www.libero.it");
				 
			      // create a urlconnection object
			    URLConnection urlConnection = null;
				urlConnection = url.openConnection();
			 
			      // wrap the urlconnection in a bufferedreader
			    BufferedReader bufferedReader = null;
                bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
			 
			    String line;
			    
			    // read from the urlconnection via the bufferedreader
   			     while ((line = bufferedReader.readLine()) != null)
			       {
			         Log.i("FILE", line);
			       }
   			     
  			     bufferedReader.close();
				}catch( IOException e){
				  	e.printStackTrace();
				}
				
				
			}	

				
			      
		};
        		
		T.scheduleAtFixedRate(Tt, Inizio, RipetiOgni);
		
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(getApplicationContext(), "AVVIO ILLSERVIZIO", Toast.LENGTH_LONG).show();	    
	    return START_STICKY;
	}    
	
	protected void onHandleIntent(Intent i)
    {
        int n=0;
        while(true)
        {
            Log.i("PROVA SERVICE", "Evento n."+n++);
            try {
                Thread.sleep(10000);
            }
            catch (InterruptedException e)
            { }
        }
    }
 
    @Override
    public void onDestroy()
    {

    }

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	

}


